{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d809c4b4",
   "metadata": {},
   "source": [
    "# 1. 百度 Access token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a0e88412",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24.a5a36688131bc68e6f1f1814f08da855.2592000.1682191363.282335-31382650\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "\n",
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token?client_id=rYG1WKktQyXwLwc3tzbGZgjx&client_secret=0X5XX1RBx3SnwWgS8DWGZvn25LuASprw&grant_type=client_credentials\"\n",
    "    \n",
    "    payload = \"\"\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    \n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.json()[\"access_token\"])\n",
    "    \n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eccc945b",
   "metadata": {},
   "source": [
    "# 2. 通用物体识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "edd12b1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '海洋', 'score': 0.819616, 'root': '自然风景-海洋'}, {'keyword': '游泳池', 'score': 0.648229, 'root': '建筑-现代建筑'}, {'keyword': '卡通动漫人物', 'score': 0.413034, 'root': '非自然图像-彩色动漫'}, {'keyword': '渡假村', 'score': 0.209955, 'root': '建筑-现代建筑'}, {'keyword': '温泉', 'score': 0.027655, 'root': '自然风景-泉'}], 'log_id': 1638986204491611286}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(r'C:\\Users\\Kookiesif_JK\\Desktop\\xd.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.d463162d4cbb2dc5ece1904b48a9aa75.2592000.1681647576.282335-31382650'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd83a038",
   "metadata": {},
   "source": [
    "# 3. 动物识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e9da4518",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 110, 'error_msg': 'Access token invalid or no longer valid'}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "动物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/animal\"\n",
    "# 二进制方式打开图片所属的[本地文件]\n",
    "f = open(r'C:\\Users\\Kookiesif_JK\\Desktop\\db.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '[调用鉴权接口获取的token]'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2390d25",
   "metadata": {},
   "source": [
    "# 4. 植物识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "764538e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 110, 'error_msg': 'Access token invalid or no longer valid'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(r'C:\\Users\\Kookiesif_JK\\Desktop\\lhh.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '[调用鉴权接口获取的token]'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40d7a54c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
